package com.hyyt.shopjsp.returnsApply.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.returnsApply.pojo.ShopReturnsApplyOplog;
import com.hyyt.shopjsp.returnsApply.pojo.ShopReturnsApply;
import com.hyyt.shopjsp.returnsApply.service.IShopReturnsApplyOplogService;
import com.hyyt.shopjsp.returnsApply.service.IShopReturnsApplyService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/06/15 0024下午 3:56.
 */

@Controller
@RequestMapping("/back/returnsApply")
public class ShopReturnsApplyController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopReturnsApplyController.class);
	private String basePath = "/back/shop/returnsApply/";
	@Resource
	private IShopReturnsApplyService shopReturnsApplyService;//退换申请Service
	@Resource
	private IShopReturnsApplyOplogService shopReturnsApplyOPLogService;//退换货申请日志service
	/**
	 * 跳转退货申请列表页面
	 *
	 * @return 返回到指定页面
	 */
	@RequestMapping("gotoReturnsApplyPage")
	public String gotoReturnsApplyPage(){
		return basePath+"returnsApplyList";
	}
	/**
	 * 退货申请列表
	 * @param returnsApplyNo 退货申请编号
	 * @param shopInfoType 店铺类型
	 * @param applyState 审核状态
	 * @param returnsState 退货状态
	 * @param page 分页
	 * @param response 返回结果集
	 */
	@RequestMapping(value="listReturnsApply",method=RequestMethod.POST)
	@ResponseBody
	public void listReturnsApply(String returnsApplyNo,String shopInfoType,String applyState,String returnsState,Page page, HttpServletResponse response){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(returnsApplyNo)){
			params.put("returnsApplyNo", returnsApplyNo.trim());
		}
		if(Utils.objectIsNotEmpty(shopInfoType)){
			params.put("shopInfoType", shopInfoType);
		}
		if(Utils.objectIsNotEmpty(applyState)){
			params.put("applyState", applyState);
		}
		if(Utils.objectIsNotEmpty(returnsState)){
			params.put("returnsState", returnsState);
		}
		int totalRecordCount =shopReturnsApplyService.countShopReturnsApply(params);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopReturnsApply> returnsApplyList  =shopReturnsApplyService.queryShopReturnsApply(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", returnsApplyList);// rows键 存放每页记录 list
		ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	 * 根据退货申请id获得对象
	 * @param returnsApplyId 退货申请id
	 * @return 返回集合
	 */
	@RequestMapping(value="getReturnsApplyObject",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getReturnsApplyObject(Integer returnsApplyId){
		if(Utils.objectIsNotEmpty(returnsApplyId)){
			Map<String, Object> params=new HashMap<>();
			params.put("returnsApplyId",returnsApplyId);
			List<ShopReturnsApply> shopReturnsApplyList=shopReturnsApplyService.queryShopReturnsApplyDetail(params);
			List<String> showUploadImgList =  new ArrayList<String>();
			for (ShopReturnsApply shopReturnsApply:shopReturnsApplyList){
				if (shopReturnsApply!=null&&shopReturnsApply.getUploadImage()!=null) {
					String[] split = shopReturnsApply.getUploadImage().split("@");
					for (String s : split) {
						showUploadImgList.add(s);
					}
				}
			}
			params.clear();
			params.put("shopReturnsApplyList",shopReturnsApplyList);
			params.put("showUploadImgList",showUploadImgList);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(params));
		}else{
			return ResultMsg.create(false);
		}
	}

	/**
	 * 查看退货申请明细
	 * @param returnsApplyId 退货申请id
	 * @param response 返回结果集
	 */
	@RequestMapping(value="getReturnsApplyOPLogList",method=RequestMethod.POST)
	@ResponseBody
	public void getReturnsApplyOPLogList(String returnsApplyId,HttpServletResponse response){
		Map<String, Object> params=new HashMap<>();
		params.put("returnsApplyNo", returnsApplyId);
		List<ShopReturnsApplyOplog> returnsApplyOPLogList = shopReturnsApplyOPLogService.queryShopReturnsAppluOPLog(params);
		params.clear();
		params.put("returnsApplyOPLogList", returnsApplyOPLogList);
		 ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	 * 根据退货申请id获得对象
	 * @param returnsApplyId 退货申请id
	 * @param applyState 审核状态
	 * @return 返回集合
	 */
	@RequestMapping(value="auditReturnsApply",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg auditReturnsApply(Integer returnsApplyId,Integer applyState){
		if(Utils.objectIsNotEmpty(returnsApplyId)&&Utils.objectIsNotEmpty(returnsApplyId)){
			ShopReturnsApply shopReturnsApply=shopReturnsApplyService.loadShopReturnsApply(returnsApplyId);
			shopReturnsApply.setApplyState(applyState);
			shopReturnsApplyService.updateShopReturnsApply(shopReturnsApply);
			return ResultMsg.create(true);
		}else{
			return ResultMsg.create(false);
		}
	}

	/**
	 * 根据退货申请id获得对象
	 * @param returnsApplyId 退货申请id
	 * @param returnsState 退换货完成状态
	 * @return 返回集合
	 */
	@RequestMapping(value="updateReturnsState",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg updateReturnsState(Integer returnsApplyId,Integer returnsState){
		if(Utils.objectIsNotEmpty(returnsApplyId)&&Utils.objectIsNotEmpty(returnsState)){
			ShopReturnsApply shopReturnsApply=shopReturnsApplyService.loadShopReturnsApply(returnsApplyId);
			shopReturnsApply.setReturnsState(returnsState);
			shopReturnsApplyService.updateShopReturnsApply(shopReturnsApply);
			return ResultMsg.create(true);
		}else{
			return ResultMsg.create(false);
		}
	}
}
